/****************************************************
 * @file: communication.c
 * @brief: 生成、分析数据
 * @author: 廖文雄 
 * @date: 2019.07.27
 * @note: 
 ****************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <malloc.h>

#include "communication.h"

char *build_data(int type)
{
    char *data = (char *)malloc(10 * sizeof(char));
    char *return_data;

    switch (type) {
        case REQUEST:
            data[0] = HEAD;
            data[1] = CODE_REQUEST;
            data[3] = END;
            data[2] = data[0] - data[1] - data[3];
            data[4] = '\0';
            break;
        case RESPONSE:
            data[0] = HEAD;
            data[1] = CODE_RESPONSE;
            data[3] = END;
            data[2] = data[0] - data[1] - data[3];
            data[4] = '\0';
            break;
        case ACK_DATA:
            data[0] = HEAD;
            data[1] = CODE_ACK_DATA;
            data[3] = END;
            data[2] = data[0] - data[1] - data[3];
            data[4] = '\0';
            break;
        case PRINT_NORMAL:
            data[0] = HEAD;
            data[1] = CODE_NORMAL;
            data[2] = 'h';
            data[3] = 'i';
            data[5] = END;
            data[4] = data[0] - data[1] - data[2] - data[3] - data[5];
            data[6] = '\0';
            break;
        case PRINT_REVERSE:
            data[0] = HEAD;
            data[1] = CODE_REVERSE;
            data[2] = 'h';
            data[3] = 'i';
            data[5] = END;
            data[4] = data[0] - data[1] - data[2] - data[3] - data[5];
            data[6] = '\0';
            break;
        case ACK_RESPONSE:
            data[0] = HEAD;
            data[1] = CODE_ACK_RESPONSE;
            data[3] = END;
            data[2] = data[0] - data[1] - data[3];
            data[4] = '\0';
            break;
        case FALSE_RESPONSE:
            data[0] = HEAD;
            data[1] = CODE_FALSE_RESPONSE;
            data[3] = END;
            data[2] = data[0] - data[1] - data[3];
            data[4] = '\0';
            break;
        case FALSE_DATA:
            data[0] = HEAD;
            data[1] = CODE_FALSE_DATA;
            data[3] = END;
            data[2] = data[0] - data[1] - data[3];
            data[4] = '\0';
            break;
        case FALSE_REQUEST:
            data[0] = HEAD;
            data[1] = CODE_FALSE_REQUEST;
            data[3] = END;
            data[2] = data[0] - data[1] - data[3];
            data[4] = '\0';
            break;
    }

    return_data = data;
    return return_data;
}

int analysis_data(char *data)
{
    char *valid = (char *)malloc(10 * sizeof(char));
    int count = 0;
    int ret = 0;

    char head = HEAD;
    char end = END;

    char code_request = CODE_REQUEST;
    char code_false_request = CODE_FALSE_REQUEST;

    char code_response = CODE_RESPONSE;
    char code_false_response = CODE_FALSE_RESPONSE;
    char code_ack_response = CODE_ACK_RESPONSE;

    char code_normal = CODE_NORMAL;
    char code_reverse = CODE_REVERSE;
    char code_false_data = CODE_FALSE_DATA;
    char code_ack_data = CODE_ACK_DATA;

    if (data[0] == head) {
        while (data[count + 1] != end) {
            valid[count] = data[count + 1];
            count++;
        }
    }

    if (valid[0] == code_request) {
        ret = REQUEST;
    } else if (valid[0] == code_response) {
        ret = RESPONSE;
    } else if (valid[0] == code_ack_data) {
        ret = ACK_DATA;
	} else if (valid[0] == code_ack_response) {
        ret = ACK_RESPONSE;
    } else if (valid[0] == code_normal) {
        count = 0;
        while (data[count + 2] != end) {
            valid[count] = data[count + 2];
            count++;
        }
        valid[count - 1] = '\0';
        ret = PRINT_NORMAL;
        printf("\n接受成功,数据为:%s\n", valid);
    } else if (valid[0] == code_reverse) {
        count = 0;
        while (data[count + 2] != end) {
            valid[count] = data[count + 2];
            count++;
        }
        valid[count - 1] = '\0';
        printf("\n接受成功,数据为:%s\n", valid);
        ret = PRINT_REVERSE;
    } else if (valid[0] == code_false_response) {
        ret = FALSE_RESPONSE;
    } else if (valid[0] == code_false_data) {
        ret = FALSE_DATA;
    } else if (valid[0] == code_false_request) {
        ret = FALSE_REQUEST;
    }

    return ret;
}

int analysis_verify(char *get_data)
{
    char result = 0;
    char end = END;
    char verify;
    int count;

    for (count = 0; get_data[count] != end; count++) {
        verify = get_data[count];
    }

    result = get_data[0];
    for (count = 1; get_data[count] != end; count++) {
        if (get_data[count] == verify) {
            continue;
        }
        result = result - get_data[count];
    }
    result = result - get_data[count];

    if (verify == result) {
        return TRUE;
    }

    return FALSE;
}